import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemUserApi } from '#/api/system/user';

import { getDeptList } from '#/api/system/dept';
import { getSimplePostList } from '#/api/system/post';
import { $t } from '#/locales';
import { DICT_TYPE, getDictOptions } from '#/utils/dict';

export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'userId',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      component: 'Input',
      fieldName: 'userName',
      label: $t('system.user.userName'),
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'userNickName',
      label: $t('system.user.userNickName'),
      rules: 'required',
    },
    {
      label: '用户密码',
      fieldName: 'userPassword',
      component: 'InputPassword',
      componentProps: {
        placeholder: '请输入用户密码',
        type: 'password',
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['userId'],
        show: (values) => !values.userId,
      },
    },
    {
      fieldName: 'deptId',
      label: $t('system.user.deptId'),
      component: 'ApiTreeSelect',
      componentProps: {
        api: async () => {
          const data = await getDeptList();
          return data;
        },
        class: 'w-full',
        labelField: 'deptName',
        valueField: 'deptId',
        childrenField: 'children',
        placeholder: '请选择归属部门',
        treeDefaultExpandAll: true,
      },
    },
    {
      fieldName: 'postIds',
      label: '岗位',
      component: 'ApiSelect',
      componentProps: {
        api: getSimplePostList,
        class: 'w-full',
        labelField: 'postName',
        valueField: 'postId',
        placeholder: '请选择岗位',
        multiple: 'multiple',
        collapseTags: 'collapse-tags',
        collapseTagsTooltip: 'collapse-tags-tooltip',
      },
    },
    {
      component: 'Input',
      fieldName: 'userEmail',
      label: $t('system.user.userEmail'),
      //   rules: 'email',
    },
    {
      component: 'Input',
      fieldName: 'userPhone',
      label: $t('system.user.userPhone'),
      //  rules: 'mobile',
    },
    {
      component: 'RadioGroup',
      fieldName: 'userSex',
      label: $t('system.user.userSex'),
      componentProps: {
        options: getDictOptions(DICT_TYPE.SYSTEM_USER_SEX, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      defaultValue: 0,
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: $t('common.enabled'), value: 1 },
          { label: $t('common.disabled'), value: 0 },
        ],
        optionType: 'button',
      },
      defaultValue: 1,
      fieldName: 'userStatus',
      label: $t('system.user.userStatus'),
    },
    {
      component: 'Textarea',
      fieldName: 'remark',
      label: $t('system.user.remark'),
    },
  ];
}

export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'userName',
      label: $t('system.user.userName'),
    },
    {
      component: 'Input',
      fieldName: 'userNickName',
      label: $t('system.user.userNickName'),
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          { label: $t('system.user.sex.male'), value: 1 },
          { label: $t('system.user.sex.female'), value: 2 },
          { label: $t('system.user.sex.unknown'), value: 0 },
        ],
      },
      fieldName: 'userSex',
      label: $t('system.user.userSex'),
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          { label: $t('common.enabled'), value: 1 },
          { label: $t('common.disabled'), value: 0 },
        ],
      },
      fieldName: 'userStatus',
      label: $t('system.user.userStatus'),
    },
    {
      component: 'RangeDatePicker',
      fieldName: 'createTime',
      label: $t('system.user.createTime'),
    },
  ];
}

export function useColumns<T = SystemUserApi.SystemUser>(
  onActionClick: OnActionClickFn<T>,
): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'userName',
      title: $t('system.user.userName'),
    },
    {
      field: 'userNickName',
      title: $t('system.user.userNickName'),
    },
    {
      field: 'deptName',
      title: $t('system.user.deptName'),
    },
    {
      cellRender: { name: 'CellTag' },
      field: 'userStatus',
      title: $t('system.user.userStatus'),
    },
    {
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.SYSTEM_USER_SEX },
      },
      field: 'userSex',
      title: $t('system.user.userSex'),
    },
    {
      field: 'createTime',
      title: $t('system.user.createTime'),
      formatter: 'formatDateTime',
      width: 180,
    },
    {
      align: 'center',
      cellRender: {
        attrs: {
          nameField: 'userNickName', // 操作列显示用昵称
          nameTitle: $t('system.user.userNickName'),
          onClick: onActionClick,
        },
        name: 'CellOperation',
      },
      field: 'operation',
      fixed: 'right',
      title: $t('system.user.operation'),
      width: 150,
    },
  ];
}
